from . import blue_app
from ..tool.myTool import domain
from spider import  imageShare_in
from spider import imageShare
from flask import request




@blue_app.route("/imageshare/type")
@domain
def getAllType():
    return imageShare.type_list

@blue_app.route("/imageshare/get",methods=["get"])
@domain
def getMainImageInfo():
    type = request.args.get("type")
    type_n = int(type)
    imageObjs = imageShare_in.getInByType(type_n)
    result = []
    for imageObj in imageObjs:
        i = {}
        i["address"] = imageObj["address"]
        i["id"] = imageObj["id"]
        i["imgUrl"] = imageObj["imgUrl"]
        i["title"] = imageObj["title"]
        result.append(i)
    return result

@blue_app.route("/imageshare/getInfo",methods=["get"])
@domain
def getHouseInfo():
    id_str = request.args.get("id")
    if not id_str:
        return {"error":"参数异常"}
    id = int(id_str)
    return imageShare_in.getHoserInfo(id)

@blue_app.route("/imageShare/getComment/<int:id>",methods=["get"])
@domain
def getImgComment(id):
    start_str = request.args.get("start")
    end_str = request.args.get("end")
    start = int(start_str)
    end = int(end_str)
    return imageShare_in.getComment(id,start,end)

@blue_app.route("/imageShare/save",methods=["post"])
@domain
def saveImgComment():
    id = int(request.form.get("id"))
    context = request.form.get("context")
    result = imageShare_in.setComment(id,None,context)
    return result